﻿using ExamWeek01.Domain;
using ExamWeek01.Service.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;

namespace ExamWeek01.API.Controllers
{
    /// <summary>
    /// 控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        /// <summary>
        /// 用户服务
        /// </summary>
        private readonly IUserService userService;
        /// <summary>
        /// 角色服务
        /// </summary>
        private readonly IRoleService roleService;
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<UserController> logger;
        /// <summary>
        /// 方法
        /// </summary>
        /// <param name="userService">用户服务</param>
        /// <param name="roleService">角色服务</param>
        /// <param name="logger">日志</param>
        public UserController(IUserService userService, IRoleService roleService, ILogger<UserController> logger)
        {
            this.userService = userService;
            this.roleService = roleService;
            this.logger = logger;
        }
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="roleName">角色名</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页码</param>
        /// <param name="totalCount">总共</param>
        /// <param name="listCount">总数量</param>
        /// <returns>列表显示</returns>
        [HttpGet]
        public IActionResult GetRoles(string? roleName, int pageIndex, int pageSize )
        {
            try
            {
                logger.LogError("【控制器】获取角色列表显示");
                int totalCount = 0;
                int listCount = 0;
                var list = roleService.GetRoles(roleName, pageIndex, pageSize, out totalCount, out listCount);
                return Ok(new { 
                    totalCount,
                    listCount,
                    list,
                });
            }
            catch (Exception ex)
            {
                logger.LogError("获取角色列表出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 用户列表显示
        /// </summary>
        /// <param name="uName">用户名</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页码</param>
        /// <param name="totalCount">总页数</param>
        /// <param name="listCount">总条数</param>
        /// <returns>列表显示</returns>
        [HttpGet]
        public IActionResult GetUser(string? uName, int pageIndex, int pageSize )
        {
            try
            {
                logger.LogError("【控制器】获取用户列表显示");
                int totalCount = 0;
                int listCount = 0;
                var list = userService.GetUser(uName, pageIndex, pageSize, out totalCount, out listCount);
                return Ok(new {
                    totalCount,
                    listCount,
                     list
                });
            }
            catch (Exception ex)
            {
                logger.LogError("获取角色列表出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="role">角色实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public IActionResult AddRole(Role role)
        {
            try
            {

               var entity= roleService.AddRole(role);
                return Ok(entity);

            }
            catch (Exception ex)
            {
                logger.LogError("添加角色信息错误" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="rId">角色id</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteRole(int rId)
        {
            try
            {
                var entity = roleService.DeleteRole(rId);
                return Ok(entity);
            }
            catch (Exception ex)
            {
                logger.LogError("删除角色信息错误" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 反填角色实体
        /// </summary>
        /// <param name="rId">角色id</param>
        /// <returns>实体</returns>
        [HttpGet]
        public IActionResult GetRoleId(int rId)
        {
            try
            {
                var entity = roleService.GetRoleId(rId);
                return Ok(entity);
            }
            catch (Exception ex)
            {
                logger.LogError("获取角色实体失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="role">角色</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public IActionResult UpdRole(Role role)
        {
            try
            {
                var entity =roleService.UpdRole(role);
                return Ok(entity);

            }
            catch (Exception ex)
            {
                logger.LogError("修改角色信息错误" + ex.Message);
                throw;
            }
        }
    }
}
